import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/application/maintenance_logs_entity.dart';
import 'package:safetymonitor/router/app_pages.dart';

/// 维护日志
class MaintainLogsController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 获取数据
  Future<List<MaintenanceLogsEntity>> getMaintenanceLogsList(
      int index, int size,
      {String? search}) async {
    final res =
        await HttpUtil().generateGetRequest<List<MaintenanceLogsEntity>>(
      Interface.deviceMaintenanceInfo,
      queryParameters: {
        'pageNo': index,
        'pageSize': size,
        'deviceId': Get.arguments,
      },
    ).execute();
    List<MaintenanceLogsEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list ?? [];
  }

  /// 新增维护日志
  void addMaintainLogs() {
    Get.toNamed(Routes.addUpdateMaintainLogs,
        arguments: {'deviceId': Get.arguments});
  }

  /// 编辑
  void onClickEdit(MaintenanceLogsEntity item) {
    Get.toNamed(Routes.addUpdateMaintainLogs,
        arguments: {'deviceId': Get.arguments, 'item': item});
  }

  /// 删除 ids=6
  void delLog(int id) async {
    final res = await HttpUtil().generateDeleteRequest(
      Interface.deleteMaintenance,
      queryParameters: {'ids': id},
    ).execute(isShowToast: false);
    res.onSuccess((result) {
      if (result.data != null) {
        AppToast.show('删除成功');
      } else {
        AppToast.show(result.message);
      }
    });
    res.onError((e) {
      AppToast.showError(e.message);
    });
    globalKey.currentState?.onRefresh();
  }
}
